package com.pms.actions;

import java.io.File;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporter;

import com.pms.utils.CalendarUtility;
import com.pms.utils.ReportUtility;
import com.pms.framework.Action;
import com.pms.framework.JSPUtility;

public class CommonReportAction implements Action{
	public void execute(HttpServletRequest request, HttpServletResponse response) {

		Hashtable<String,String> error = new Hashtable<String,String>() ;
		
		try{
			
			String strFDate = "";
			String strTDate = "";
			
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");			
			Date date = new Date();
			
			if(request.getParameter("fromDate") == null) {
				strFDate = formatter.format(date);
			} else {
				strFDate = request.getParameter("fromDate");
			}
			
			if(request.getParameter("toDate") == null) {
				strTDate = formatter.format(date);
			} else {
				strTDate = request.getParameter("toDate");
			}
			
			System.out.println("@@@@@ strFDate == "+ strFDate);
			System.out.println("@@@@@ strTDate == "+strTDate);
			
			String strFromDate =  strFDate + " 00:00:00";
			String strToDate =  strTDate + " 23:59:59";
			
			Timestamp fromDate = CalendarUtility.generateDate(strFromDate);
			Timestamp toDate = CalendarUtility.generateDate(strToDate);
				
			int reportType=JSPUtility.GetInt(request, "reportType",1);
			int reportIndx=JSPUtility.GetInt(request, "reportIndx",0);
			int csvReport = JSPUtility.GetInt(request, "csvReport",0);
			
			
			System.out.println("@@@@@@@@@@ reportType ==> "+reportType);
			System.out.println("@@@@@@@@@@ reportIndx ==> "+reportIndx);
			System.out.println("@@@@@@@@@@ csvReport ==> "+csvReport);
			
			HashMap<String ,Object> map = new HashMap<String ,Object>();
			
			String parentReportname = "";
			String[] subReportname = null;
			
			File reportFile = null;
			File subReportFile = null;		
			
			ArrayList<File> subReportList = null;
			
			ServletContext context = request.getSession().getServletContext();
			String realContextPath = context.getRealPath("/");
			realContextPath = realContextPath.replace("\\", "/");
			
			switch(reportIndx) {
			// CashManagement Report
			case 1:
				if(reportType==1){
					
					parentReportname = realContextPath + "jasper/CashManagementByOrderDate.jasper";
				}else{
					parentReportname = realContextPath + "jasper/CashManagementByPaymentDate.jasper";
				}
				break;
				
				// MiscJobs Report
			case 2:
				if(reportType==1){
					parentReportname = realContextPath + "jasper/MiscJobsByOrderDate.jasper";
				}else{
					parentReportname = realContextPath + "jasper/MiscJobsByPaymentDate.jasper";
				}
				break;
				
				// PrintJobsByOrderDate Report
			case 3:
				if(reportType==1){
					parentReportname = realContextPath + "jasper/PrintJobsByOrderDate.jasper";
				}else{
					parentReportname = realContextPath + "jasper/PrintJobsByPaymentDate.jasper";
				}
				break;
				
				//OutStandings Report
			case 4:
				if(reportType==1){
					parentReportname = realContextPath + "jasper/OutStandingByOrderDate.jasper";
				}else{
					parentReportname = realContextPath + "jasper/OutStandingByPaymentDate.jasper";
				}
				
				break;

				// Refund Report
			case 5:
				if(reportType==1){
					parentReportname = realContextPath + "jasper/RefundByOrderDate.jasper";
				}else{
					parentReportname = realContextPath + "jasper/RefundByPaymentDate.jasper";
				}
				break;
			case 6:
				if(reportType==1){
					parentReportname = realContextPath + "jasper/BadDebtsByOrderDate .jasper";
				}else{
					parentReportname = realContextPath + "jasper/BadDebtsByPaymentDate.jasper";
				}
				break;
			case 8:
				if(reportType== -1){
				parentReportname = realContextPath + "jasper/MyCollectionReports.jasper";
				//String[] subReportFile = new String[2];
				subReportname =new String[2];
				subReportname[0] = realContextPath + "jasper/MyCollectionSubReport.jasper";
				subReportname[1] = realContextPath + "jasper/BalanceDueSubReport.jasper";
				}
				break;

			case 9:
				if(reportType== -1){
				parentReportname = realContextPath + "jasper/CanceledSalesOrderReports.jasper";
				}
				break;

			}
			
            System.out.println("@@@@@@@@@@ parentReportname ==> "+parentReportname);
			reportFile = new File(parentReportname);		
			
			if(subReportname != null && subReportname.length>0){
				subReportList = new ArrayList<File>();
				for(int i=0 ; i< subReportname.length ;i++){
					subReportFile = new File(subReportname[i]);
					System.out.println("@@@@@@@@@@ subReportFile ==> "+subReportFile);
					subReportList.add(subReportFile);
				}
			}
			
			map.put("FromDate",fromDate);
			map.put("ToDate",toDate);
			
			response.setContentType("text/html");
			request.setAttribute("nextPageToken", "success");

			ReportUtility report = new ReportUtility();
			System.out.println("@@@@@@@@@@ Report ==> "+report);

			if(csvReport == 1){
				JRCsvExporter csvExporter = null;
				if(subReportname != null && subReportname.length>0){
					System.out.println("@@@@@@@@@@ In Sub CSV Report ==> ");
					csvExporter = report.generateCSVReportWithSubReport(map, reportFile, response, request, subReportFile);
					
				} else {
					System.out.println("@@@@@@@@@@ In Main CSV Report ==> ");
					csvExporter = report.generateCSVReport(map, reportFile, response, request);
				}
				csvExporter.exportReport();
			}else{
				JRHtmlExporter exporter = null;    	
				if(subReportname != null && subReportname.length>0){
					System.out.println("@@@@@@@@@@ In Sub Report ==> ");
					exporter = report.generateHTMLReportWithSubReport(map, reportFile, response.getWriter(), request ,subReportList);
				} else {
					System.out.println("@@@@@@@@@@ In Main Report ==> ");
					exporter = report.generateHTMLReport(map, reportFile, response.getWriter(), request );
				}
				exporter.exportReport();
			}
			
			
		}catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("Common Report Action Catch");
			error.put("error", "System is not behaving properly. Contact Tech Support.");
			request.setAttribute("error", error);
			request.setAttribute("nextPageToken", "failure");
		}
	}
}
